﻿using System;
using Autofac;
using Microsoft.RuleEngine;

namespace AppFx.BizTalk.Container.Loaders
{
    public class BreLoader : IContainerLoader
    {
        public void Load(ContainerBuilder containerBuilder, string loaderKey)
        {
            var policyName = loaderKey;
            var policy = new Policy(policyName);

            var containerContents = new ContainerContents();

            var facts = new object[1];
            facts[0] = containerContents;
            try
            {
                policy.Execute(facts);
            }
            catch (Exception ex)
            {
                var message = string.Format("The load application container rule errored with message: {0}", ex.Message);
                throw new ApplicationException(message, ex);
            }
            finally
            {
                policy.Dispose();
            }

            containerBuilder.InstallFromAssemblies(containerContents.Assemblies.ToArray());
        }
    }
}
